package com.linciping.useges.plugin.view;

import com.intellij.openapi.util.Iconable;
import com.intellij.psi.PsiNamedElement;
import com.intellij.util.PsiIconUtil;
import com.linciping.useges.plugin.bean.UsegesNode;

import javax.swing.*;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;

public class UsegesTreeCellRendererV2 extends DefaultTreeCellRenderer {

    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
        UsegesNode usegesNode = (UsegesNode) value;
        PsiNamedElement psiNamedElement = usegesNode.getPsiNamedElement();
        Icon icon = psiNamedElement.getIcon(Iconable.ICON_FLAG_VISIBILITY);
        if (icon == null) {
            icon = PsiIconUtil.getProvidersIcon(usegesNode.getPsiNamedElement(), Iconable.ICON_FLAG_VISIBILITY);
        }
        if (!tree.isEnabled()) {
            setEnabled(false);
            setDisabledIcon(icon);
        } else {
            setEnabled(true);
            setIcon(icon);
        }
        return this;
    }
}
